<template>
  <div class="activity-registration">
    <div class="page-header">
      <div class="container">
        <div class="breadcrumb">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item @click="$router.push('/activities')">活动中心</el-breadcrumb-item>
            <el-breadcrumb-item>活动报名</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <h1 class="page-title">活动报名</h1>
        <p class="page-subtitle">参与展会精彩活动，获取前沿技术资讯</p>
      </div>
    </div>
    
    <div class="container">
      <!-- 活动信息 -->
      <section class="activity-info-section" v-if="selectedActivity">
        <div class="activity-card">
          <div class="activity-image">
            <img :src="selectedActivity.image" :alt="selectedActivity.title">
            <div class="activity-type-badge">{{ selectedActivity.type }}</div>
          </div>
          <div class="activity-details">
            <h2>{{ selectedActivity.title }}</h2>
            <p class="activity-description">{{ selectedActivity.description }}</p>
            <div class="activity-meta">
              <div class="meta-item">
                <el-icon><Clock /></el-icon>
                <span>{{ selectedActivity.time }}</span>
              </div>
              <div class="meta-item">
                <el-icon><Location /></el-icon>
                <span>{{ selectedActivity.location }}</span>
              </div>
              <div class="meta-item">
                <el-icon><User /></el-icon>
                <span>限额 {{ selectedActivity.capacity }} 人</span>
              </div>
              <div class="meta-item">
                <el-icon><Tickets /></el-icon>
                <span>已报名 {{ selectedActivity.registered }} 人</span>
              </div>
            </div>
            <div class="activity-speakers" v-if="selectedActivity.speakers">
              <h4>演讲嘉宾</h4>
              <div class="speakers-list">
                <span v-for="speaker in selectedActivity.speakers" :key="speaker" class="speaker-tag">
                  {{ speaker }}
                </span>
              </div>
            </div>
          </div>
        </div>
      </section>

      <!-- 报名表单 -->
      <section class="registration-form-section">
        <h2 class="section-title">报名信息</h2>
        <div class="form-container">
          <el-form :model="registrationForm" :rules="registrationRules" ref="registrationFormRef" label-width="120px">
            <div class="form-section">
              <h3>基本信息</h3>
              <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="姓名" prop="name">
                    <el-input v-model="registrationForm.name" placeholder="请输入您的姓名" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="联系电话" prop="phone">
                    <el-input v-model="registrationForm.phone" placeholder="请输入联系电话" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="邮箱地址" prop="email">
                    <el-input v-model="registrationForm.email" placeholder="请输入邮箱地址" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="职位" prop="position">
                    <el-input v-model="registrationForm.position" placeholder="请输入您的职位" />
                  </el-form-item>
                </el-col>
              </el-row>
            </div>

            <div class="form-section">
              <h3>公司信息</h3>
              <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="公司名称" prop="company">
                    <el-input v-model="registrationForm.company" placeholder="请输入公司名称" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="所属行业" prop="industry">
                    <el-select v-model="registrationForm.industry" placeholder="请选择所属行业" style="width: 100%">
                      <el-option label="制造业" value="manufacturing" />
                      <el-option label="信息技术" value="it" />
                      <el-option label="金融服务" value="finance" />
                      <el-option label="医疗健康" value="healthcare" />
                      <el-option label="教育培训" value="education" />
                      <el-option label="其他" value="other" />
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="公司规模" prop="companySize">
                    <el-select v-model="registrationForm.companySize" placeholder="请选择公司规模" style="width: 100%">
                      <el-option label="1-50人" value="small" />
                      <el-option label="51-200人" value="medium" />
                      <el-option label="201-1000人" value="large" />
                      <el-option label="1000人以上" value="enterprise" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="城市" prop="city">
                    <el-input v-model="registrationForm.city" placeholder="请输入所在城市" />
                  </el-form-item>
                </el-col>
              </el-row>
            </div>

            <div class="form-section">
              <h3>参与信息</h3>
              <el-form-item label="参与目的" prop="purpose">
                <el-checkbox-group v-model="registrationForm.purpose">
                  <el-checkbox label="technology">了解新技术</el-checkbox>
                  <el-checkbox label="networking">商务对接</el-checkbox>
                  <el-checkbox label="learning">学习交流</el-checkbox>
                  <el-checkbox label="partnership">寻求合作</el-checkbox>
                  <el-checkbox label="investment">投资机会</el-checkbox>
                </el-checkbox-group>
              </el-form-item>
              <el-form-item label="感兴趣话题" prop="interests">
                <el-select 
                  v-model="registrationForm.interests" 
                  multiple 
                  placeholder="请选择感兴趣的话题"
                  style="width: 100%"
                >
                  <el-option label="人工智能" value="ai" />
                  <el-option label="工业4.0" value="industry4" />
                  <el-option label="智能制造" value="smart_manufacturing" />
                  <el-option label="机器人技术" value="robotics" />
                  <el-option label="物联网" value="iot" />
                  <el-option label="大数据" value="big_data" />
                  <el-option label="云计算" value="cloud" />
                  <el-option label="区块链" value="blockchain" />
                </el-select>
              </el-form-item>
              <el-form-item label="特殊需求" prop="specialRequests">
                <el-input
                  v-model="registrationForm.specialRequests"
                  type="textarea"
                  :rows="4"
                  placeholder="请输入特殊需求（如饮食要求、无障碍设施等）"
                />
              </el-form-item>
            </div>

            <div class="form-section">
              <h3>确认信息</h3>
              <div class="registration-summary">
                <div class="summary-item">
                  <span>活动名称：</span>
                  <span>{{ selectedActivity?.title }}</span>
                </div>
                <div class="summary-item">
                  <span>活动时间：</span>
                  <span>{{ selectedActivity?.time }}</span>
                </div>
                <div class="summary-item">
                  <span>活动地点：</span>
                  <span>{{ selectedActivity?.location }}</span>
                </div>
                <div class="summary-item">
                  <span>报名费用：</span>
                  <span class="free-tag">免费</span>
                </div>
              </div>
              <el-form-item prop="agreement">
                <el-checkbox v-model="registrationForm.agreement">
                  我已阅读并同意<a href="#" class="agreement-link">《活动参与协议》</a>和<a href="#" class="agreement-link">《隐私政策》</a>
                </el-checkbox>
              </el-form-item>
            </div>

            <div class="form-actions">
              <el-button size="large" @click="resetForm">重置表单</el-button>
              <el-button type="primary" size="large" @click="submitRegistration" :loading="submitting">
                确认报名
              </el-button>
            </div>
          </el-form>
        </div>
      </section>

      <!-- 活动推荐 -->
      <section class="recommended-activities-section">
        <h2 class="section-title">推荐活动</h2>
        <div class="activities-grid">
          <div class="activity-card" v-for="activity in recommendedActivities" :key="activity.id">
            <div class="activity-image">
              <img :src="activity.image" :alt="activity.title">
            </div>
            <div class="activity-info">
              <h4>{{ activity.title }}</h4>
              <p>{{ activity.time }}</p>
              <p>{{ activity.location }}</p>
              <el-button type="text" @click="selectActivity(activity)">
                立即报名
              </el-button>
            </div>
          </div>
        </div>
      </section>
    </div>

    <!-- 成功对话框 -->
    <el-dialog v-model="successDialogVisible" title="报名成功" width="500px" center>
      <div class="success-content">
        <el-icon class="success-icon"><SuccessFilled /></el-icon>
        <h3>报名成功！</h3>
        <p>您已成功报名参加"{{ selectedActivity?.title }}"活动</p>
        <div class="next-steps">
          <h4>接下来：</h4>
          <ul>
            <li>我们将在24小时内发送确认邮件</li>
            <li>活动前3天会发送提醒通知</li>
            <li>请准时参加活动，现场签到</li>
          </ul>
        </div>
      </div>
      <template #footer>
        <el-button @click="successDialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="$router.push('/activities')">返回活动中心</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, reactive, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { 
  Clock, 
  Location, 
  User, 
  Tickets,
  SuccessFilled 
} from '@element-plus/icons-vue'

export default {
  name: 'ActivityRegistration',
  setup() {
    const route = useRoute()
    const router = useRouter()
    
    const selectedActivity = ref(null)
    const registrationFormRef = ref()
    const submitting = ref(false)
    const successDialogVisible = ref(false)

    // 报名表单
    const registrationForm = reactive({
      name: '',
      phone: '',
      email: '',
      position: '',
      company: '',
      industry: '',
      companySize: '',
      city: '',
      purpose: [],
      interests: [],
      specialRequests: '',
      agreement: false
    })

    // 表单验证规则
    const registrationRules = {
      name: [
        { required: true, message: '请输入姓名', trigger: 'blur' }
      ],
      phone: [
        { required: true, message: '请输入联系电话', trigger: 'blur' },
        { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
      ],
      email: [
        { required: true, message: '请输入邮箱地址', trigger: 'blur' },
        { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
      ],
      position: [
        { required: true, message: '请输入职位', trigger: 'blur' }
      ],
      company: [
        { required: true, message: '请输入公司名称', trigger: 'blur' }
      ],
      industry: [
        { required: true, message: '请选择所属行业', trigger: 'change' }
      ],
      companySize: [
        { required: true, message: '请选择公司规模', trigger: 'change' }
      ],
      city: [
        { required: true, message: '请输入所在城市', trigger: 'blur' }
      ],
      purpose: [
        { type: 'array', required: true, message: '请选择参与目的', trigger: 'change' }
      ],
      agreement: [
        { validator: (rule, value, callback) => {
          if (!value) {
            callback(new Error('请同意活动参与协议'))
          } else {
            callback()
          }
        }, trigger: 'change' }
      ]
    }

    // 模拟活动数据
    const mockActivities = {
      1: {
        id: 1,
        title: '人工智能与机器人技术发展论坛',
        type: '技术论坛',
        description: '邀请业内专家分享AI和机器人技术的最新发展趋势，探讨技术创新和应用前景。',
        time: '2025年3月15日 14:00-17:00',
        location: '广州国际会展中心 会议室A',
        capacity: 200,
        registered: 156,
        image: 'https://images.unsplash.com/photo-1485827404703-89b55fcc595e?w=400&h=300&fit=crop',
        speakers: ['张教授', '李博士', '王总工程师']
      },
      2: {
        id: 2,
        title: '智能制造解决方案发布会',
        type: '产品发布',
        description: '多家企业发布最新智能制造解决方案，展示工业4.0时代的创新成果。',
        time: '2025年3月16日 10:00-12:00',
        location: '广州国际会展中心 发布厅',
        capacity: 300,
        registered: 89,
        image: 'https://images.unsplash.com/photo-1581094794329-c8112a89af12?w=400&h=300&fit=crop',
        speakers: ['优必选科技CEO', 'ABB中国总经理', '库卡机器人CTO']
      },
      3: {
        id: 3,
        title: '工业机器人应用案例分享',
        type: '技术交流',
        description: '分享工业机器人在不同行业的成功应用案例，探讨实施经验和技术要点。',
        time: '2025年3月17日 09:00-11:00',
        location: '广州国际会展中心 会议室B',
        capacity: 150,
        registered: 124,
        image: 'https://images.unsplash.com/photo-1560472354-b33ff0c44a43?w=400&h=300&fit=crop',
        speakers: ['行业专家团队']
      }
    }

    // 推荐活动数据
    const recommendedActivities = ref([
      {
        id: 2,
        title: '智能制造解决方案发布会',
        time: '2025年3月16日 10:00-12:00',
        location: '发布厅',
        image: 'https://images.unsplash.com/photo-1581094794329-c8112a89af12?w=300&h=200&fit=crop'
      },
      {
        id: 3,
        title: '工业机器人应用案例分享',
        time: '2025年3月17日 09:00-11:00',
        location: '会议室B',
        image: 'https://images.unsplash.com/photo-1560472354-b33ff0c44a43?w=300&h=200&fit=crop'
      }
    ])

    // 加载活动信息
    const loadActivity = () => {
      const activityId = route.query.id || route.query.activityId || '1'
      const activity = mockActivities[activityId]
      if (activity) {
        selectedActivity.value = activity
        // 过滤推荐活动，排除当前活动
        recommendedActivities.value = recommendedActivities.value.filter(
          item => item.id != activityId
        )
      } else {
        // 默认显示第一个活动
        selectedActivity.value = mockActivities[1]
      }
    }

    // 选择活动
    const selectActivity = (activity) => {
      router.push(`/activity-registration?id=${activity.id}`)
    }

    // 重置表单
    const resetForm = () => {
      Object.assign(registrationForm, {
        name: '',
        phone: '',
        email: '',
        position: '',
        company: '',
        industry: '',
        companySize: '',
        city: '',
        purpose: [],
        interests: [],
        specialRequests: '',
        agreement: false
      })
      if (registrationFormRef.value) {
        registrationFormRef.value.resetFields()
      }
    }

    // 提交报名
    const submitRegistration = async () => {
      if (!registrationFormRef.value) return
      
      try {
        await registrationFormRef.value.validate()
        
        // 检查活动容量
        if (selectedActivity.value.registered >= selectedActivity.value.capacity) {
          ElMessage.error('抱歉，该活动报名人数已满')
          return
        }

        submitting.value = true
        
        // 调用真实的后端接口
        const response = await subpageApi.submitActivityRegistration({
          activityId: selectedActivity.value.id,
          activityName: selectedActivity.value.title,
          ...registrationForm,
          registrationType: 'activity',
          submitTime: new Date().toISOString()
        })
        
        if (response && response.code === '200') {
          submitting.value = false
          successDialogVisible.value = true
          
          // 更新报名人数
          selectedActivity.value.registered += 1
        } else {
          ElMessage.error(response?.msg || '活动报名提交失败，请重试')
        }
        
      } catch (error) {
        console.error('活动报名提交失败:', error)
        ElMessage.error(error.message || '活动报名提交失败，请重试')
      } finally {
        submitting.value = false
      }
    }

    onMounted(() => {
      loadActivity()
    })

    return {
      selectedActivity,
      registrationForm,
      registrationRules,
      registrationFormRef,
      submitting,
      successDialogVisible,
      recommendedActivities,
      selectActivity,
      resetForm,
      submitRegistration
    }
  }
}
</script>

<style scoped>
.activity-registration {
  min-height: 100vh;
  background: linear-gradient(135deg, #320063 0%, #4a0a8a 100%);
  color: white;
}

.page-header {
  padding: 60px 0 40px;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
}

.breadcrumb {
  margin-bottom: 20px;
}

.breadcrumb :deep(.el-breadcrumb__inner) {
  color: white;
  cursor: pointer;
}

.breadcrumb :deep(.el-breadcrumb__inner:hover) {
  color: #ff6b35;
}

.page-title {
  font-size: 2.5rem;
  font-weight: bold;
  margin-bottom: 10px;
  background: linear-gradient(45deg, #fff, #e0e0ff);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.page-subtitle {
  font-size: 1.1rem;
  opacity: 0.9;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.section-title {
  font-size: 1.8rem;
  font-weight: bold;
  margin-bottom: 30px;
  text-align: center;
  color: white;
}

/* 活动信息部分 */
.activity-info-section {
  padding: 40px 0;
}

.activity-card {
  display: grid;
  grid-template-columns: 400px 1fr;
  gap: 30px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 15px;
  padding: 30px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.activity-image {
  position: relative;
  border-radius: 10px;
  overflow: hidden;
}

.activity-image img {
  width: 100%;
  height: 250px;
  object-fit: cover;
}

.activity-type-badge {
  position: absolute;
  top: 15px;
  right: 15px;
  background: #ff6b35;
  color: white;
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 0.8rem;
  font-weight: bold;
}

.activity-details h2 {
  font-size: 1.8rem;
  font-weight: bold;
  margin-bottom: 15px;
  color: white;
}

.activity-description {
  font-size: 1rem;
  line-height: 1.6;
  margin-bottom: 20px;
  opacity: 0.9;
}

.activity-meta {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 15px;
  margin-bottom: 25px;
}

.meta-item {
  display: flex;
  align-items: center;
  font-size: 0.9rem;
}

.meta-item .el-icon {
  margin-right: 8px;
  color: #ff6b35;
}

.activity-speakers h4 {
  font-size: 1.1rem;
  font-weight: bold;
  margin-bottom: 10px;
  color: #ff6b35;
}

.speakers-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.speaker-tag {
  background: rgba(255, 107, 53, 0.2);
  color: #ff6b35;
  padding: 4px 12px;
  border-radius: 15px;
  font-size: 0.8rem;
  border: 1px solid rgba(255, 107, 53, 0.3);
}

/* 报名表单部分 */
.registration-form-section {
  padding: 40px 0;
  border-top: 1px solid rgba(255, 255, 255, 0.2);
}

.form-container {
  max-width: 800px;
  margin: 0 auto;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 15px;
  padding: 40px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.form-section {
  margin-bottom: 40px;
}

.form-section h3 {
  font-size: 1.3rem;
  font-weight: bold;
  margin-bottom: 20px;
  color: #ff6b35;
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
  padding-bottom: 10px;
}

.registration-summary {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  padding: 20px;
  margin-bottom: 20px;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.summary-item {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
  font-size: 1rem;
}

.summary-item:last-child {
  margin-bottom: 0;
}

.free-tag {
  background: #67c23a;
  color: white;
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 0.8rem;
  font-weight: bold;
}

.agreement-link {
  color: #ff6b35;
  text-decoration: none;
}

.agreement-link:hover {
  text-decoration: underline;
}

.form-actions {
  text-align: center;
  margin-top: 30px;
}

.form-actions .el-button {
  margin: 0 10px;
  padding: 12px 30px;
  font-size: 1rem;
}

/* 推荐活动部分 */
.recommended-activities-section {
  padding: 40px 0;
  border-top: 1px solid rgba(255, 255, 255, 0.2);
}

.activities-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
}

.recommended-activities-section .activity-card {
  display: block;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.2);
  transition: transform 0.3s ease;
}

.recommended-activities-section .activity-card:hover {
  transform: translateY(-5px);
}

.recommended-activities-section .activity-image {
  height: 150px;
}

.recommended-activities-section .activity-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.activity-info {
  padding: 20px;
}

.activity-info h4 {
  font-size: 1.1rem;
  font-weight: bold;
  margin-bottom: 10px;
}

.activity-info p {
  font-size: 0.9rem;
  opacity: 0.8;
  margin-bottom: 5px;
}

/* 成功对话框 */
.success-content {
  text-align: center;
  padding: 20px;
}

.success-icon {
  font-size: 4rem;
  color: #67c23a;
  margin-bottom: 20px;
}

.success-content h3 {
  font-size: 1.5rem;
  font-weight: bold;
  margin-bottom: 15px;
  color: #67c23a;
}

.success-content p {
  font-size: 1rem;
  margin-bottom: 20px;
  opacity: 0.9;
}

.next-steps {
  text-align: left;
  background: rgba(103, 194, 58, 0.1);
  border-radius: 10px;
  padding: 20px;
  border: 1px solid rgba(103, 194, 58, 0.3);
}

.next-steps h4 {
  font-size: 1.1rem;
  font-weight: bold;
  margin-bottom: 10px;
  color: #67c23a;
}

.next-steps ul {
  list-style: none;
  padding: 0;
}

.next-steps li {
  padding: 5px 0;
  padding-left: 20px;
  position: relative;
}

.next-steps li::before {
  content: '✓';
  position: absolute;
  left: 0;
  color: #67c23a;
  font-weight: bold;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .activity-card {
    grid-template-columns: 1fr;
    gap: 20px;
  }
  
  .activity-meta {
    grid-template-columns: 1fr;
  }
  
  .form-container {
    padding: 20px;
  }
  
  .page-title {
    font-size: 2rem;
  }
}

/* Element Plus 组件样式覆盖 */
.activity-registration :deep(.el-form-item__label) {
  color: white !important;
  font-weight: 500;
}

.activity-registration :deep(.el-input__wrapper) {
  background: rgba(255, 255, 255, 0.1) !important;
  border: 1px solid rgba(255, 255, 255, 0.3) !important;
  color: white !important;
}

.activity-registration :deep(.el-input__inner) {
  color: white !important;
}

.activity-registration :deep(.el-select .el-input__wrapper) {
  background: rgba(255, 255, 255, 0.1) !important;
  border: 1px solid rgba(255, 255, 255, 0.3) !important;
}

.activity-registration :deep(.el-textarea__inner) {
  background: rgba(255, 255, 255, 0.1) !important;
  border: 1px solid rgba(255, 255, 255, 0.3) !important;
  color: white !important;
}

.activity-registration :deep(.el-checkbox__label) {
  color: white !important;
}

.activity-registration :deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
  background-color: #ff6b35 !important;
  border-color: #ff6b35 !important;
}

.activity-registration :deep(.el-checkbox__inner:hover) {
  border-color: #ff6b35 !important;
}
</style>
